package pl.nazaweb.imedproplus.rest;

import java.util.List;
import pl.nazaweb.imedproplus.rest.pojo.ResponsePojo;

public abstract class AbstractRest<T> {

    public abstract List<T> getEntityPojoListResult();

    public abstract void persistNewEnities(List<T> pojos);

    public abstract void removeEnities(List<T> pojos);

    public abstract void updateEnities(List<T> pojos);

    public ResponsePojo getEntityPojoList() {
        ResponsePojo response = new ResponsePojo();
        try {
            response.data = this.getEntityPojoListResult();
            response.success = true;
        } catch (Exception e) {
            response.success = false;
            response.message = e.getMessage();
        }
        return response;
    }

    public ResponsePojo createEntities(List<T> pojos) {
        ResponsePojo response = new ResponsePojo();
        try {
            this.persistNewEnities(pojos);
        } catch (Exception e) {
            response.success = false;
            response.message = e.getMessage();
        }
        return response;
    }

    public ResponsePojo destroyEntities(List<T> pojos) {
        ResponsePojo response = new ResponsePojo();
        try {
            this.removeEnities(pojos);
        } catch (Exception e) {
            response.success = false;
            response.message = e.getMessage();
        }
        return response;
    }

    public ResponsePojo updateEntities(List<T> pojos) {
        ResponsePojo response = new ResponsePojo();
        try {
            this.updateEnities(pojos);
        } catch (Exception e) {
            response.success = false;
            response.message = e.getMessage();
        }
        return response;
    }
}
